package com.wave.web.controller.system;

import com.wave.common.core.controller.BaseController;
import com.wave.common.core.domain.R;
import com.wave.common.utils.StringUtils;
import com.wave.common.utils.redis.RedisUtils;
import com.wave.system.domain.SysMechanicalArmInfo;
import com.wave.system.runner.SystemApplicationRunner;
import com.wave.system.service.ISysProduceRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * 系统首页
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/index")
public class IndexController  extends BaseController {

    private final ISysProduceRecordService sysProduceRecordService;


    /**
     * 查询今日产量趋势
     */
    @GetMapping("/getDailyOutputTrend")
    public R<Map<String,Object>> getDailyOutputTrend(String dateStr) {
        return R.ok(sysProduceRecordService.getDailyOutputTrendNew(dateStr));
    }


    /**
     * 查询机械臂信息
     */
    @GetMapping("/getMechanicalArmInfo")
    public R<List<SysMechanicalArmInfo>> getMechanicalArmInfo() {
        return R.ok(SystemApplicationRunner.armList);
    }

    /**
     * 查询系统运行状态
     */
    @GetMapping("/getSystemData")
    public R<List<Map<String, Object>>> getSystemData() {
        return R.ok(sysProduceRecordService.getSystemData());
    }

    /**
     * 手动设置sn码
     *
     * @param sn sn码
     */
    @GetMapping("/setsn/{type}/{sn}")
    public R<Void> setsn(@PathVariable int type, @PathVariable String sn) {
        if(StringUtils.isEmpty(sn)) {
            return R.fail("当前sn为空，设置失败");
        }
        if(sn.length()!=18) {
            return R.fail("设置失败，原因是sn码必须为18位");
        }
        String sncodeCache = "";
        if(type == 1){
            RedisUtils.setCacheObject("sncodecache", sn, Duration.ofMinutes(5));
            sncodeCache = RedisUtils.getCacheObject("sncodecache");
            log.info("手动设置的sn为：{}", sncodeCache);
            return R.ok("设置当前物料sn码成功");
        }
        if(type == 2){
            RedisUtils.setCacheObject("a1ccdsncodecache", sn, Duration.ofMinutes(5));
            sncodeCache = RedisUtils.getCacheObject("a1ccdsncodecache");
            log.info("手动设置a1机器人ccd拍照的sn码为：{}", sncodeCache);
            return R.ok("设置a1机器人ccd拍照的sn码成功");
        }
        if(type == 3){
            RedisUtils.setCacheObject("a3ccdsncodecache", sn, Duration.ofMinutes(5));
            sncodeCache = RedisUtils.getCacheObject("a3ccdsncodecache");
            log.info("手动设置a3机器人ccd拍照的sn码为：{}", sncodeCache);
            return R.ok("设置a3机器人ccd拍照的sn码成功");
        }

        return R.ok("设置当前物料sn成功");
    }
}
